package cn.test;

/**
 * @ClassName AverageCapital
 * @Description TODO
 * @Author 郑万富
 * @Date 2020/11/9 10:34
 * @Version 1.0
 */

import javax.swing.*;

/**
 * 等额本金类
 * 等额本息是指一种贷款的还款方式，指在还款期内，每月偿还同等数额的贷款(包括本金和利息)。
 * 变量名：贷款本金：loanMoney; 年利率：yearRate; 还款总月数：totalMonth;
 */
/*
public class AverageCapital {
    private double loanMoney;
    private double yearRate;
    private double totalMonth;

}
*/
public class AverageCapital {
    public static void main (String[] args) {
// 用户输入贷款总量
        String loanMoney = JOptionPane.showInputDialog("请输入贷款金额：");
        double loanAmount = Double.parseDouble(loanMoney);

// 用户输入年利率
        String yearRate = JOptionPane.showInputDialog("请输入贷款年利率（如0.36）：");
        double annualInterestRate = Double.parseDouble(yearRate);
// 计算月利率
        double monthlyInterestRate = annualInterestRate / 1200;

// 用户输入贷款年限
        String numberOfYearsStr = JOptionPane.showInputDialog("请输入贷款年限：");
        int numberOfYears = Integer.parseInt(numberOfYearsStr);

// 计算每月还款数额和总还款数额
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
        double totalPayment = monthlyPayment * 12 * numberOfYears;

// 将payment格式化为小数点后面的两位，采用向下取整的方式
        monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
        totalPayment = (int)(totalPayment * 100) / 100.0;

//输出结果，totalPayment 以及 monthlyPayment
        String output = "月还款量：" + monthlyPayment + "\n总还款量：" + totalPayment;
        JOptionPane.showMessageDialog(null, output);
    }
}
